home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / g__~1 / tests09.zoo / tests / tstring.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-02  |  8.2 KB  |  411 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2.  
  3. /*
  4.  A test file for Strings
  5. */
  6.  
  7. #include <xstring.h>
  8. #include <std.h>
  9. #include <assert.h>
  10.  
  11. extern "C" long _stksize = 16*1024;
  12.  
  13. // can't nicely echo assertions because they contain quotes
  14.  
  15. #define tassert(ex) {if (!(ex)) \
  16.                      { cerr << "failed assertion at " << __LINE__ << "\n"; \
  17.                        abort(); } }
  18.  
  19.   String X = "Hello";
  20.   String Y = "world";
  21.   String N = "123";
  22.   String c;
  23.   char*  s = ",";
  24.   Regex  r = "e[a-z]*o";
  25.  
  26. void decltest()
  27. {
  28.   String x;
  29.   cout << "an empty String:" << x << "\n";
  30.   assert(x.OK());
  31.   assert(x == "");
  32.  
  33.   String y = "Hello";
  34.   cout << "A string initialized to Hello:" << y << "\n";
  35.   assert(y.OK());
  36.   assert(y == "Hello");
  37.  
  38.   if (y[y.length()-1] == 'o')
  39.     y = y + '\n';
  40.   assert(y == "Hello\n");
  41.   y = "Hello";
  42.  
  43.   String a = y;
  44.   cout << "A string initialized to previous string:" << a << "\n";
  45.   assert(a.OK());
  46.   assert(a == "Hello");
  47.   assert(a == y);
  48.  
  49.   String b (a.at(1, 2));
  50.   cout << "A string initialized to previous string.at(1, 2):" << b << "\n";
  51.   assert(b.OK());
  52.   assert(b == "el");
  53.  
  54.   char ch = '@';
  55.   String z(ch); 
  56.   cout << "A string initialized to @:" << z << "\n";
  57.   assert(z.OK());
  58.   assert(z == "@");
  59.  
  60.   String n = dec(20);
  61.   cout << "A string initialized to dec(20):" << n << "\n";
  62.   assert(n.OK());
  63.   assert(n == "20");
  64.  
  65.   int i = atoi(n);
  66.   double f = atof(n);
  67.   cout << "n = " << n << " atoi(n) = " << i << " atof(n) = " << f << "\n";
  68.   assert(i == 20);
  69.   assert(f == 20);
  70.  
  71.   assert(X.OK());
  72.   assert(Y.OK());
  73.   assert(x.OK());
  74.   assert(y.OK());
  75.   assert(z.OK());
  76.   assert(n.OK());
  77.   assert(r.OK());
  78. }
  79.  
  80. void cattest()
  81. {
  82.   String x = X;
  83.   String y = Y;
  84.   String z = x + y;
  85.   cout << "z = x + y = " << z << "\n";
  86.   assert(x.OK());
  87.   assert(y.OK());
  88.   assert(z.OK());
  89.   assert(z == "Helloworld");
  90.  
  91.   x += y;
  92.   cout << "x += y; x = " << x << "\n";
  93.   assert(x.OK());
  94.   assert(y.OK());
  95.   assert(x == "Helloworld");
  96.  
  97.   y = Y;
  98.   x = X;
  99.   y.prepend(x);
  100.   cout << "y.prepend(x); y = " << y << "\n";
  101.   assert(y == "Helloworld");
  102.  
  103.   y = Y;
  104.   x = X;
  105.   cat(x, y, x, x);
  106.   cout << "cat(x, y, x, x); x = " << x << "\n";
  107.   assert(x == "HelloworldHello");
  108.  
  109.   y = Y;
  110.   x = X;
  111.   cat(y, x, x, x);
  112.   cout << "cat(y, x, x, x); x = " << x << "\n";
  113.   assert(x == "worldHelloHello");
  114.  
  115.   x = X;
  116.   y = Y;
  117.   z = x + s + ' ' + y.at("w") + y.after("w") + ".";
  118.   cout << "z = x + s +  + y.at(w) + y.after(w) + . = " << z << "\n";
  119.   assert(z.OK());
  120.   assert(z == "Hello, world.");
  121.  
  122. }
  123.  
  124. void comparetest()
  125. {  
  126.   String x = X;
  127.   String y = Y;
  128.   String n = N;
  129.   String z = x + y;
  130.  
  131.   assert(x != y);
  132.   assert(x == "Hello");
  133.   assert(x != z.at(0, 4));
  134.   assert (x < y);
  135.   assert(!(x >= z.at(0, 6)));
  136.   assert(x.contains("He"));
  137.   assert (z.contains(x));
  138.   assert(x.contains(r));
  139.  
  140.   assert(!(x.matches(r)));
  141.   assert(x.matches(RXalpha));
  142.   assert(!(n.matches(RXalpha)));
  143.   assert(n.matches(RXint));
  144.   assert(n.matches(RXdouble));
  145.  
  146.   assert(x.index("lo") == 3);
  147.   assert(x.index("l", 2) == 2);
  148.   assert(x.index("l", -1) == 3);
  149.   assert(x.index(r)  == 1);
  150.   assert(x.index(r, -2) == 1);
  151.  
  152.   assert(x.contains("el", 1));
  153.   assert(x.contains("el"));
  154.  
  155.   assert(common_prefix(x, "Help") == "Hel");
  156.   assert(common_suffix(x, "to") == "o");
  157.  
  158.   assert(fcompare(x, "hELlo") == 0);
  159.   assert(fcompare(x, "hElp") < 0);
  160. }
  161.  
  162. void substrtest()
  163. {
  164.   String x = X;
  165.  
  166.   char ch = x[0];
  167.   cout << "ch = x[0] = " << ch << "\n";
  168.   assert(ch == 'H');
  169.  
  170.   String z = x.at(2, 3);
  171.   cout << "z = x.at(2, 3) = " << z << "\n";
  172.   assert(z.OK());
  173.   assert(z == "llo");
  174.  
  175.   x.at(2, 2) = "r";
  176.   cout << "x.at(2, 2) = r; x = " << x << "\n";
  177.   assert(x.OK());
  178.   assert(x.at(2,2).OK());
  179.   assert(x == "Hero");
  180.  
  181.   x = X;
  182.   x.at(0, 1) = "j";
  183.   cout << "x.at(0, 1) = j; x = " << x << "\n";
  184.   assert(x.OK());
  185.   assert(x == "jello");
  186.  
  187.   x = X;
  188.   x.at("He") = "je";
  189.   cout << "x.at(He) = je; x = " << x << "\n";
  190.   assert(x.OK());
  191.   assert(x == "jello");
  192.  
  193.   x = X;
  194.   x.at("l", -1) = "i";
  195.   cout << "x.at(l, -1) = i; x = " << x << "\n";
  196.   assert(x.OK());
  197.   assert(x == "Helio");
  198.   
  199.   x = X;
  200.   z = x.at(r);
  201.   cout << "z = x.at(r) = " << z << "\n";
  202.   assert(z.OK());
  203.   assert(z == "ello");
  204.  
  205.   z = x.before("o");
  206.   cout << "z = x.before(o) = " << z << "\n";
  207.   assert(z.OK());
  208.   assert(z == "Hell");
  209.  
  210.   x.before("ll") = "Bri";
  211.   cout << "x.before(ll) = Bri; x = " << x << "\n";
  212.   assert(x.OK());
  213.   assert(x == "Brillo");
  214.  
  215.   x = X;
  216.   z = x.before(2);
  217.   cout << "z = x.before(2) = " << z << "\n";
  218.   assert(z.OK());
  219.   assert(z == "He");
  220.  
  221.   z = x.after("Hel");
  222.   cout << "z = x.after(Hel) = " << z << "\n";
  223.   assert(z.OK());
  224.   assert(z == "lo");
  225.  
  226.   x.after("Hel") = "p";  
  227.   cout << "x.after(Hel) = p; x = " << x << "\n";
  228.   assert(x.OK());
  229.   assert(x == "Help");
  230.  
  231.   x = X;
  232.   z = x.after(3);
  233.   cout << "z = x.after(3) = " << z << "\n";
  234.   assert(z.OK());
  235.   assert(z == "o");
  236.  
  237.   z = "  a bc";
  238.   z  = z.after(RXwhite);
  239.   cout << "z =   a bc; z = z.after(RXwhite); z =" << z << "\n";
  240.   assert(z.OK());
  241.   assert(z == "a bc");
  242. }
  243.  
  244.  
  245. void utiltest()
  246. {
  247.   String x = X;
  248.   int matches = x.gsub("l", "ll");
  249.   cout << "x.gsub(l, ll); x = " << x << "\n";
  250.   assert(x.OK());
  251.   assert(matches == 2);
  252.   assert(x == "Hellllo");
  253.  
  254.   x = X;
  255.   assert(x.OK());
  256.   matches = x.gsub(r, "ello should have been replaced by this string");
  257.   assert(x.OK());
  258.   cout << "x.gsub(r, ...); x = " << x << "\n";
  259.   assert(x.OK());
  260.   assert(matches == 1);
  261.   assert(x == "Hello should have been replaced by this string");
  262.  
  263.   matches = x.gsub(RXwhite, "#");
  264.   cout << "x.gsub(RXwhite, #); x = " << x << "\n";
  265.   assert(matches == 7);
  266.   assert(x.OK());
  267.  
  268.   String z = X + Y;
  269.   z.del("loworl");
  270.   cout << "z = x+y; z.del(loworl); z = " << z << "\n";
  271.   assert(z.OK());
  272.   assert(z == "Held");
  273.  
  274.   x = X;
  275.   z = reverse(x);
  276.   cout << "reverse(x) = " << z << "\n";
  277.   assert(z.OK());
  278.   assert(z == "olleH");
  279.  
  280.   x.reverse();
  281.   cout << "x.reverse() = " << x << "\n";
  282.   assert(x.OK());
  283.   assert(x == z);
  284.  
  285.   x = X;
  286.   z = upcase(x);
  287.   cout << "upcase(x) = " << z << "\n";
  288.   assert(z.OK());
  289.   assert(z == "HELLO");
  290.  
  291.   z = downcase(x);
  292.   cout << "downcase(x) = " << z << "\n";
  293.   assert(z.OK());
  294.   assert(z == "hello");
  295.  
  296.   z = capitalize(x);
  297.   cout << "capitalize(x) = " << z << "\n";
  298.   assert(z.OK());
  299.   assert(z == "Hello");
  300.   
  301.   z = replicate('*', 10);
  302.   cout << "z = replicate(*, 10) = " << z << "\n";
  303.   assert(z.OK());
  304.   assert(z == "**********");
  305.   assert(z.length() == 10);
  306. }
  307.  
  308. void splittest()
  309. {
  310.   String z = "This string\thas\nfive words";
  311.   cout << "z = " << z << "\n";
  312.   String w[10];
  313.   int nw = split(z, w, 10, RXwhite);
  314.   assert(nw == 5);
  315.   cout << "from split(z, RXwhite, w, 10), n words = " << nw << ":\n";
  316.   for (int i = 0; i < nw; ++i)
  317.   {
  318.     assert(w[i].OK());
  319.     cout << w[i] << "\n";
  320.   }
  321.   assert(w[0] == "This");
  322.   assert(w[1] == "string");
  323.   assert(w[2] == "has");
  324.   assert(w[3] == "five");
  325.   assert(w[4] == "words");
  326.   assert(w[5] == 0);
  327.  
  328.   z = join(w, nw, "/");
  329.   cout << "z = join(w, nw, /); z =" << z << "\n";
  330.   assert(z.OK());
  331.   assert(z == "This/string/has/five/words");
  332. }
  333.  
  334.  
  335. void iotest()
  336. {
  337.   String z;
  338.   cout << "enter a word:";
  339.   cin >> z;
  340.   cout << "word =" << z << " ";
  341.   cout << "length = " << z.length() << "\n";
  342. }
  343.  
  344. void identitytest(String a, String b)
  345. {
  346.   String x = a;
  347.   String y = b;
  348.   x += b;
  349.   y.prepend(a);
  350.   assert((a + b) == x);
  351.   assert((a + b) == y);
  352.   assert(x == y);
  353.   assert(x.after(a) == b);
  354.   assert(x.before(b, -1) == a);
  355.   assert(x.from(a) == x);
  356.   assert(x.through(b, -1) == x);
  357.   assert(x.at(a) == a);
  358.   assert(x.at(b) == b);
  359.  
  360.   assert(reverse(x) == reverse(b) + reverse(a));
  361.   
  362.   assert((a + b + a) == (a + (b + a)));
  363.  
  364.   x.del(b, -1);
  365.   assert(x == a);
  366.  
  367.   y.before(b, -1) = b;
  368.   assert(y == (b + b));
  369.   y.at(b) = a;
  370.   assert(y == (a + b));
  371.  
  372.   x = a + reverse(a);
  373.   for (int i = 0; i < 7; ++i)
  374.   {
  375.     y = x;
  376.     x += x;
  377.     assert(x.OK());
  378.     assert(x == reverse(x));
  379.     assert(x.index(y) == 0);
  380.     assert(x.index(y, -1) == x.length() / 2);
  381.   }
  382. }
  383.  
  384. void freqtest()
  385. {
  386.   String x = "Hello World";
  387.   SubString y = x.at(0,5);
  388.   assert(x.freq('l') == 3);    // char
  389.   assert(x.freq("lo") == 1);    // char*
  390.   assert(x.freq(x) == 1);    // String
  391.   assert(x.freq(y) == 1);    // SubString
  392. }
  393.  
  394. main()
  395. {
  396.   decltest();
  397.   cattest();
  398.   comparetest();
  399.   substrtest();
  400.   utiltest();
  401.   splittest();
  402.   freqtest();
  403.   identitytest(X, X);
  404.   identitytest(X, Y);
  405.   identitytest(X+Y+N+X+Y+N, "A string that will be used in identitytest but is otherwise just another useless string.");
  406.   iotest();
  407.   cout << "\nEnd of test\n";
  408. }
  409.  
  410.  
  411.